using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BattleCity.GameLogic;
using Microsoft.FSharp.Core;

#if WINDOWS_PHONE
using PhoneInput;
#endif


namespace BattleCity.Rendering
{
   
    public class Renderer : Microsoft.Xna.Framework.DrawableGameComponent
    {
        //new Model declaration
        GridRenderer gridRenderer;
        BlocksRenderer blocksRenderer;
        TanksRenderer tanksRenderer;
        BulletsRenderer bulletsRenderer;
        BonusRenderer bonusRenderer;
        BackgroundRenderer backgroundRenderer;
        HudRenderer hudRenderer;

        //Texture declaration
        Texture2D materialsTex;

      

        /************************************ OOOOOOOOOOOOOOOOOOOOOOLDDDDDDDDDDDDDDDDDDDDDDDD*////////////// */
        int fps;
        float frameCounter;
        int capturedFps;

        SpriteFont debugFont;

        //Model declaration      
       
        Texture2D bonusTexture;
     
        Texture2D bulletTexture;
        Texture2D ebulletTexture;
        Texture2D padButton;
        Texture2D fireButton;
        Texture2D eyeButton;

        //Graphics Utility declaration
        SpriteBatch spriteBatch;
        BasicEffect effect;

        int screenWidth;

        public Renderer(SpaceTankGame game)
            : base(game)
        {
            screenWidth = game.graphics.PreferredBackBufferWidth;
        }

       
        public override void Initialize()
        {
            frameCounter = 0;
            fps = 0;
            capturedFps = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            //Graphics settings loading
            spriteBatch = new SpriteBatch(GraphicsDevice);
            effect = new BasicEffect(GraphicsDevice);

            #region effect configuration
            #if WINDOWS_PHONE
            effect.View = Matrix.CreateLookAt(new Vector3(GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.X, 25, GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.Z + 15),
                        new Vector3(GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.X, 0, GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.Z), -Vector3.UnitZ);
      
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(0.95f, GraphicsDevice.Viewport.AspectRatio, 1, 1000);
            #else
            effect.View = Matrix.CreateLookAt(new Vector3(0, 45, 25), new Vector3(0, 0, 5), Vector3.UnitY);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(0.95f, GraphicsDevice.Viewport.AspectRatio, 1, 1000);
            #endif


            effect.DirectionalLight0.DiffuseColor = new Vector3(0.8f, 0.8f, 0.8f);
            effect.DirectionalLight0.Direction = new Vector3(0, -1, 0);
            effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
            #endregion

            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

            //Texture loading
            materialsTex = Game.Content.Load<Texture2D>(@"block_textures\material_tex");

            bulletTexture = Game.Content.Load<Texture2D>(@"bullet_texture");
            ebulletTexture = Game.Content.Load<Texture2D>(@"ebullet_texture");

            padButton = Game.Content.Load<Texture2D>(@"TouchStick\pad_texture");
            fireButton = Game.Content.Load<Texture2D>(@"TouchStick\firebutton_texture");
            eyeButton = Game.Content.Load<Texture2D>(@"TouchStick\eyebutton_texture");

            bonusTexture = Game.Content.Load<Texture2D>(@"other_textures\bonus_texture");

            
            //Renderer initialization
            gridRenderer = new GridRenderer(GraphicsDevice);
            blocksRenderer = new BlocksRenderer(GraphicsDevice);
            tanksRenderer = new TanksRenderer(Game.Content, effect.Projection);
            bulletsRenderer = new BulletsRenderer(Game.Content, GraphicsDevice, ref effect);
            bonusRenderer = new BonusRenderer(GraphicsDevice);
            hudRenderer = new HudRenderer(Game.Content, GraphicsDevice, screenWidth);
            backgroundRenderer = new BackgroundRenderer(Game.Content, GraphicsDevice);

            #if WINDOWS_PHONE
            TouchStick.state = new TouchStick(new Vector2(150, 340), padButton.Width, fireButton.Width, eyeButton.Width);
            #endif

            debugFont = Game.Content.Load<SpriteFont>(@"debug");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            #if WINDOWS_PHONE
            if (!GameState.bannerOnScreen.Value)
            {
                if (TouchStick.state.getPressedButton()[1] == TouchStickButton.Eye)
                {
                    effect.View = Matrix.CreateLookAt(new Vector3(0,55,0),new Vector3(0,0,0), -Vector3.UnitZ);
                }
                else
                {
                    effect.View = Matrix.CreateLookAt(new Vector3(GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.X, 25, GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.Z + 15),
                        new Vector3(GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.X, 0, GameState.gameState.PlayerTank.Value.Position.xnaVector3YView.Z), -Vector3.UnitZ);
                }
            }
        #endif

            blocksRenderer.Update();
            tanksRenderer.Update(effect.View);
         
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            
            GraphicsDevice.Clear(Color.Black);

            fps++;
            frameCounter += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (frameCounter > 1000.0f)
            {
                capturedFps = fps;
                fps = 0;
                frameCounter = 0;
            }

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            //GRID AND BLOCKS RENDERING
            effect.World = Matrix.Identity;
            effect.LightingEnabled = true;
            effect.TextureEnabled = true;
            effect.Texture = materialsTex;
            effect.CurrentTechnique.Passes[0].Apply();

            gridRenderer.Draw();
            blocksRenderer.Draw();
           
            //TANKS AND BULLETS RENDERING
            effect.LightingEnabled = false;
            effect.Texture = bulletsRenderer.bulletsTexture;

#if WINDOWS_PHONE
            if (TouchStick.state.getPressedButton()[1] == TouchStickButton.Eye) tanksRenderer.Draw(true);
            else
#endif
            tanksRenderer.Draw(false);
            bulletsRenderer.Draw(ref effect);
                  
            //BONUS RENDERING
            effect.LightingEnabled = true;
            effect.Texture = bonusTexture;

            bonusRenderer.Draw(ref effect, gameTime);

            backgroundRenderer.Draw();

            hudRenderer.Draw(spriteBatch);
         
            
            #if DEBUG
            spriteBatch.Begin();
            spriteBatch.DrawString(debugFont, capturedFps.ToString(), new Vector2(20, 20), Color.Red);
            spriteBatch.DrawString(debugFont, GameStateUpdater.capturedFps.ToString(), new Vector2(700, 20), Color.White);
             spriteBatch.End();
            #endif


#if WINDOWS_PHONE
            if (!GameState.bannerOnScreen.Value)
            {
                if ((GameState.gameState.DestroyedEnemies.Value != 20) && (GameState.gameState.PlayerLives.Value != 0))
                    TouchStick.state.Draw(spriteBatch, padButton, fireButton, eyeButton);
            }
#endif
            GraphicsDevice.Indices = null;
         
            base.Draw(gameTime);
        }
    }
}
